#!/bin/sh

e2e_test_install() {
  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" "2"

  kubectl create configmap -n "$CLUSTER_NAMESPACE" pgadmin --from-literal=config_distro.py="$(cat << EOF
CA_FILE = '/etc/ssl/certs/ca-certificates.crt'
LOG_FILE = '/dev/null'
HELP_PATH = '../../docs'
DEFAULT_BINARY_PATHS = {
        'pg': '/usr/local/pgsql-14',
        'pg-14': '/usr/local/pgsql-14',
        'pg-13': '/usr/local/pgsql-13',
        'pg-12': '/usr/local/pgsql-12',
        'pg-11': '/usr/local/pgsql-11',
        'pg-10': '/usr/local/pgsql-10'
}
EOF
    )"

  deploy_curl_pod "$CLUSTER_NAMESPACE"

  wait_pods_running "$CLUSTER_NAMESPACE" 3
  wait_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"
}

e2e_test() {
  run_test "Checking that custom container is running as expected" custom_container_check
}

custom_container_check() {
  local RESULT EXIT_CODE
  try_function wait_until eval 'kubectl exec -n "$CLUSTER_NAMESPACE" curl -- \
      curl -v -s "http://$CLUSTER_NAME.$CLUSTER_NAMESPACE:9000/login" \
      --retry 5 --retry-delay 2 --retry-connrefused \
      | grep -qF "pgadmin_header_logo"'
  if "$RESULT"
  then
    success "PgAdmin container is running as expected and is aviable on primary service"
  else
    fail "PgAdmin container is not running as expected or is not aviable on primary service"
  fi

  try_function wait_until eval 'kubectl exec -n "$CLUSTER_NAMESPACE" curl -- \
      curl -v -s "http://$CLUSTER_NAME-replicas.$CLUSTER_NAMESPACE:9000/login" \
      --retry 5 --retry-delay 2 --retry-connrefused \
      | grep -qF "pgadmin_header_logo"'
  if "$RESULT"
  then
    success "PgAdmin container is running as expected and is aviable on replicas service"
  else
    fail "PgAdmin container is not running as expected or is not aviable on replicas service"
  fi

  try_function wait_until eval 'kubectl exec -n "$CLUSTER_NAMESPACE" curl -- \
      curl -v -s "http://$CLUSTER_NAME.$CLUSTER_NAMESPACE:3000/pg_stat_activity" \
      --retry 5 --retry-delay 2 --retry-connrefused \
      | jq -r ".[].backend_type" \
      | grep -qxF "walsender"'
  if "$RESULT"
  then
    success "PostgRest container is running as expected and is aviable on primary service"
  else
    fail "PostgRest container is not running as expected or is not aviable on primary service"
  fi

  try_function wait_until eval 'kubectl exec -n "$CLUSTER_NAMESPACE" curl -- \
      curl -v -s "http://$CLUSTER_NAME-replicas.$CLUSTER_NAMESPACE:3000/pg_stat_activity" \
      --retry 5 --retry-delay 2 --retry-connrefused \
      | jq -r ".[].backend_type" \
      | grep -qxF "walreceiver"'
  if "$RESULT"
  then
    success "PostgRest container is running as expected and is aviable on replicas service"
  else
    fail "PostgRest container is not running as expected or is not aviable on replicas service"
  fi

  kubectl delete pod -n "$CLUSTER_NAMESPACE" \
    -l "app=StackGresCluster,stackgres.io/cluster-name=$CLUSTER_NAME,stackgres.io/cluster=true"

  try_function wait_until eval 'kubectl logs -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-0" -c custom-pg-controldata \
      | grep -qx "Database cluster state:\s\+shut down"'
  if "$RESULT"
  then
    success "pg_controldata init container is running as expected and is aviable on primary service"
  else
    fail "pg_controldata init container is not running as expected or is not aviable on primary service"
  fi

  try_function wait_until eval 'kubectl logs -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-1" -c custom-pg-controldata \
      | grep -qx "Database cluster state:\s\+shut down in recovery"'
  if "$RESULT"
  then
    success "pg_controldata init container is running as expected and is aviable on primary service"
  else
    fail "pg_controldata init container is not running as expected or is not aviable on primary service"
  fi
}
